package com.lxy.mychat1.Adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxy.mychat1.Bean.Msg;
import com.lxy.mychat1.R;

import java.util.List;

public class MsgAdapter extends ArrayAdapter<Msg> {
    int resourceid;
    public MsgAdapter(@NonNull Context context, int resource, @NonNull List<Msg> objects) {
        super(context, resource, objects);
        this.resourceid=resource;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceid, null);
        }
        LinearLayout left_layout = view.findViewById(R.id.left_layout);
        LinearLayout right_Layout = view.findViewById(R.id.right_Layout);
        TextView left_msg = view.findViewById(R.id.left_msg);
        TextView right_msg = view.findViewById(R.id.right_msg);
        Msg msg = getItem(position);
        SharedPreferences sp = getContext().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        int myid = sp.getInt("myid", -1);
        if (myid == msg.getSendid()) {
            left_layout.setVisibility(View.GONE);
            right_Layout.setVisibility(View.VISIBLE);
            right_msg.setText(msg.getContent());
        } else {
            right_Layout.setVisibility(View.GONE);
            left_layout.setVisibility(View.VISIBLE);
            left_msg.setText(msg.getContent());
        }
        return view;
    }
}
